package com.thinksns.sociax.t4.android.function;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.db.UserSqlHelper;
import com.thinksns.sociax.t4.android.ActivityHome;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsActivity;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.android.login.ActivityBindThirdLoginUser;
import com.thinksns.sociax.t4.android.video.Logger;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelEventDetail;
import com.thinksns.sociax.t4.model.ModelImageAttach;
import com.thinksns.sociax.t4.model.ModelInformationCateList;
import com.thinksns.sociax.t4.model.ModelPost;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.t4.model.ModelVideo;
import com.thinksns.sociax.t4.model.ModelWeibo;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient.HttpResponseListener;
import com.thinksns.sociax.thinksnsbase.utils.PrefUtils;
import com.thinksns.sociax.thinksnsbase.utils.UnitSociax;

import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 类说明： 第三方登录/注册/绑定/分享相关
 *
 * @author wz
 * @version 1.0
 * @date 2014-12-5
 */
public class FunctionThirdPlatForm extends FunctionSoicax {
    private static final int MSG_ERR = 1;
    private static final int MSG_SUCCESS = 2;
    private static final int MSG_CANCEL = 3;
    private static final int MSG_REG = 4;
    private final Context mContext;
    private Platform pf;                // 操作平台
    //4.5版本修改获取第三方登录的QQ头像url
    private HashMap<String, Object> data = new HashMap();//第三方登录返回的信息用于取出QQ的头像
    private OnekeyShare mOnekeyShare; // 升级ShareSDK，换用 OneKeyShare 处理 7.10.2017
    private String mHost;

    /**
     * 获取当前操作的平台
     *
     * @return
     */
    public Platform getPlatform() {
        return pf;
    }

    /**
     * 需要注意的是必须先initShareSDK
     *
     * @param context
     * @param platform 平台 ，使用ShareSDK.getPlaform获取
     */
    public FunctionThirdPlatForm(Context context, Platform platform) {
        super(context);

        this.mContext = context;
        String[] configHost = context.getResources().getStringArray(
                R.array.site_url);
        mHost = configHost[0];
        pf = platform;
        pf.SSOSetting(false);// 设置成true直接使用网页授权，否则先考虑使用目标客户端授权，没有客户端情况下使用网页授权
        mOnekeyShare = new OnekeyShare();
        mOnekeyShare.setPlatform(pf.getName());
        //关闭sso授权
//        mOnekeyShare.disableSSOWhenAuthorize();
    }

    @Override
    protected void initUiHandler() {
        handlerUI = new UIHandler();
    }

    @Override
    protected void initActivtyHandler() {
        handlerActivity = new ActivityHandler(thread.getLooper(), context);
    }

    /**
     * 执行登录
     * 新浪登陆失败请检查回调页面，回调页面仅使用网站地址就可以
     * 微信登陆失败请检查md5签名是否当前md5签名
     */
    public void doLogin() {
        // 设置授权的监听事件
        if (pf.isAuthValid()) {
            // 取消上一个用户的授权，否则会直接跳过授权，使用上一个用户进入主页面
            pf.removeAccount(true);
        }

        ShareSDK.removeCookieOnAuthorize(true);
        pf.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                if (listener != null)
                    listener.onTaskError();
                Message msg = new Message();
                msg.what = StaticInApp.DO_THIRD_LOGIN;
                msg.obj = arg2.getMessage();
                msg.arg1 = MSG_ERR;
                handlerActivity.sendMessage(msg);

                String[] platInfo = new String[3];
                platInfo[0] = getTypeName(arg0);
                if (platInfo[0] == null)
                    return;
                platInfo[1] = arg0.getDb().getUserId();
                System.out.print("login_id" + arg0.getDb().getUserId());
                platInfo[2] = arg0.getDb().getToken();


                Log.v("thirdLogin", "dologin/onError/plat info[0]/" + platInfo[0] + "/info[1]/" + platInfo[1] + "/info[2]/" + platInfo[2]
                        + "/arg1/" + arg1 + "/arg2/" + arg2.toString());
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                if (listener != null)
                    listener.onTaskSuccess();
                String[] platInfo = new String[3];
                platInfo[0] = getTypeName(arg0);
                if (platInfo[0] == null)
                    return;
                platInfo[1] = arg0.getDb().getUserId();
                platInfo[2] = arg0.getDb().getToken();

                Message msg = new Message();
                msg.what = StaticInApp.DO_THIRD_LOGIN;
                FunctionThirdPlatForm.this.pf = arg0;
                FunctionThirdPlatForm.this.data = arg2;//4.5版本修改获取第三方登录的QQ头像url
                msg.arg1 = MSG_SUCCESS;
                handlerActivity.sendMessage(msg);

                Log.v("thirdLogin", "dologin/onComplete/plat info[0]/" + platInfo[0] + "/info[1]/" + platInfo[1] + "/info[2]/" + platInfo[2]
                        + "/arg1/" + arg1 + "/arg2/" + arg2.toString());
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                if (listener != null)
                    listener.onTaskCancle();
                Message msg = new Message();
                msg.what = StaticInApp.DO_THIRD_LOGIN;
                msg.obj = arg0;
                msg.arg1 = MSG_CANCEL;
                handlerActivity.sendMessage(msg);

                Log.v("thirdLogin", "onCancel/");
            }
        });
        // 执行授权动作
        pf.showUser(null);
    }

    /**
     * 获取平台对应到T4服务器的名称
     *
     * @param pf2
     * @return
     */
    private static String getTypeName(Platform pf2) {
        String typename = "";// 对应到T4服务器接口的名称
        if (pf2.getName().equals("QZone")) {
            typename = "qzone";
        } else if (pf2.getName().equals("SinaWeibo")) {
            typename = "sina";
        } else if (pf2.getName().equals(Wechat.NAME)) {
            typename = "weixin";
        }
        if (typename.equals("")) {
            return null;
        } else {
            return typename;
        }
    }

    /**
     * 取消授权
     */
    public void doRemoveOauth() {
        if (pf.isAuthValid()) {
            // 如果已经完成授权
            pf.removeAccount(true);
        }
    }

    /**
     * 绑定授权
     */
    public void doBindOauth() {
        // 设置授权的监听事件
        if (pf.isAuthValid()) {
            // 取消上一个用户的授权，否则会直接跳过授权，使用上一个用户进入主页面
            pf.removeAccount(true);
        }
        ShareSDK.removeCookieOnAuthorize(true);
        pf.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                Message msg = new Message();
                msg.what = StaticInApp.DO_THIRD_BIND;
                msg.obj = arg2.getMessage();
                msg.arg1 = MSG_ERR;
                handlerUI.sendMessage(msg);
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                Platform platform = arg0;
                int index = arg1;
                HashMap<String, Object> map = arg2;

                Message msg = new Message();
                msg.what = StaticInApp.DO_THIRD_BIND;
                msg.obj = arg0;
                msg.arg1 = MSG_SUCCESS;
                handlerUI.sendMessage(msg);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                Message msg = new Message();
                msg.what = StaticInApp.DO_THIRD_BIND;
                msg.obj = arg0;
                msg.arg1 = MSG_CANCEL;
                handlerUI.sendMessage(msg);
            }
        });
        // 执行授权动作
        pf.authorize();
    }

    public class UIHandler extends Handler {
        public UIHandler() {
            super();
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Thinksns app = (Thinksns) context.getApplicationContext();
            if (msg.what == StaticInApp.DO_THIRD_LOGIN) {// 执行第三方登陆
                if (listener != null)
                    listener.onTaskCancle();
                switch (msg.arg1) {
                    case MSG_SUCCESS:// 登录成功
                        Intent intent = new Intent(context, ActivityHome.class);
                        context.startActivity(intent);
                        ((Activity) context).finish();
                        //关闭首页
                        ThinksnsActivity.getInstance().finish();
                        break;
                    case MSG_ERR:// 登录失败

                        break;
                    case MSG_REG:
                        // 登录成功但是需要注册
                        Intent intent1 = new Intent(context, ActivityBindThirdLoginUser.class);
                        intent1.putExtra("type", getTypeName(pf));
                        // 微信传的是 unionid 作为 type_uid,其他为openid  2/21/2017 leiyan
                        if (pf.getName().equals(Wechat.NAME)) {
                            intent1.putExtra("type_uid", pf.getDb().get("unionid"));
                        } else {
                            intent1.putExtra("type_uid", pf.getDb().getUserId());
                        }
                        intent1.putExtra("access_token", pf.getDb().getToken());

                        String icon_url = "";
                        //4.5版本修改获取第三方登录的QQ头像url
                        if (FunctionThirdPlatForm.this.data.containsKey("figureurl_qq_2")) {
                            icon_url = (String) FunctionThirdPlatForm.this.data.get("figureurl_qq_2");
                        } else {
                            icon_url = pf.getDb().getUserIcon();
                        }

                        intent1.putExtra("icon", icon_url);
                        intent1.putExtra("name", pf.getDb().getUserName());
                        /*微信直接调用这个方法返回 null，所以增加为 null 判断就从 DB 通过 key 取值 4/5/2017 leiyan*/
                        String gender = pf.getDb().getUserGender();
                        if (TextUtils.isEmpty(gender)) {
                            gender = pf.getDb().get("gender");
                        }
                        intent1.putExtra("gender", gender);

                        context.startActivity(intent1);
                        break;
                }
            } else if (msg.what == StaticInApp.DO_THIRD_SHARE) {
                switch (msg.arg1) {
                    case MSG_SUCCESS:// 成功
                        Toast.makeText(context, "发送成功", Toast.LENGTH_SHORT).show();
                        break;
                    case MSG_ERR:// 失败
                        Toast.makeText(context, "发送失败", Toast.LENGTH_SHORT).show();
                        break;
                    case MSG_REG:// 取消
                        Toast.makeText(context, "取消发送", Toast.LENGTH_SHORT).show();
                        break;
                }

            } else if (msg.what == StaticInApp.DO_THIRD_BIND) {
                //绑定账号
                switch (msg.arg1) {
                    case MSG_SUCCESS:// 绑定成功
                        if (listener != null) {
                            listener.onTaskSuccess();
                        }
                        break;
                    case MSG_ERR:// 绑定失败
                        if (listener != null) {
                            listener.onTaskError();
                        }
                        break;
                    case MSG_REG:// 取消绑定
                        if (listener != null) {
                            listener.onTaskCancle();
                        }
                        break;
                }
            }
        }
    }

    /**
     * 用于授权的Handler回调
     */
    private class ActivityHandler extends Handler {
        private Context context = null;

        public ActivityHandler(Looper looper, Context context) {
            super(looper);
            this.context = context;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == StaticInApp.DO_THIRD_LOGIN) {
                // 执行第三方授权，处理结果
                switch (msg.arg1) {
                    case MSG_CANCEL:
                        //取消授权
                        listener.onTaskCancle();
                        break;
                    case MSG_SUCCESS:
                        listener.onTaskSuccess();
                        // 授权成功，取出授权用户信息，调用接口判断是否已经完成注册
                        try {
                            Message oauthMessage = new Message();
                            oauthMessage.what = StaticInApp.GET_THIRD_REG_INFO;
                            String[] platInfo = new String[4];
                            platInfo[0] = getTypeName(pf);
                            if (platInfo[0] == null)
                                return;
                            platInfo[1] = pf.getDb().getUserId();
                            platInfo[2] = pf.getDb().getToken();
                            // 微信传的是 unionid ,其他为openid
                            if (Wechat.NAME.equals(pf.getName())) {
                                platInfo[1] = pf.getDb().get("unionid");
                                platInfo[3] = pf.getDb().getUserId();
                            }
                            Object result = app.getOauth().getThirdRegInfo(platInfo);
                            oauthMessage.obj = result;
                            this.sendMessage(oauthMessage);
                        } catch (Exception e) {
                            listener.onTaskCancle();
                            e.printStackTrace();
                        }
                        break;
                    case MSG_ERR:
                        if (msg.obj != null) {
                            Toast.makeText(context, msg.obj.toString(),
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, "登陆失败",
                                    Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            } else if (msg.what == StaticInApp.GET_THIRD_REG_INFO) {
                // 调用接口获取第三方注册信息
                Message regMessage = new Message();
                regMessage.what = StaticInApp.DO_THIRD_LOGIN;
                regMessage.arg1 = MSG_ERR;
                Object result = msg.obj;
                if (result == null) {
                    Toast.makeText(context, "登录失败，请检查网络", Toast.LENGTH_SHORT).show();
                    listener.onTaskCancle();
                    return;
                }
                try {
                    JSONObject regInfo = new JSONObject(result.toString());
                    if (regInfo.has("status")) {
                        int status = regInfo.optInt("status");
                        /*1:需要注册 -1：后台禁止登录 0：其他错误 6.1.2017 leiyan*/
                        if (status == 1) {
                            // 账号未注册调到注册页面
                            regMessage.arg1 = MSG_REG;
                        } else {
                            // 后台禁止登陆
                            regMessage.arg1 = MSG_ERR;
                            ToastUtils.showToast(regInfo.optString("msg"));
                        }
                    } else if (regInfo.has("uid")) {
                        //已经绑定过第三方账号
                        String token = regInfo.getString("oauth_token");
                        String tokenSecret = regInfo.getString("oauth_token_secret");
                        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(tokenSecret)) {
                            ModelUser authorizeResult = new ModelUser(regInfo.getInt("uid"),
                                    "", "", token, tokenSecret);
                            ApiHttpClient.TOKEN_SECRET = tokenSecret;
                            ApiHttpClient.TOKEN = token;
                            //这里可以保存用户基础信息，跳转到首页之后再获取用户更详细资料

                            app.getUsers().show(authorizeResult, userListener);
                        } else {
                            Toast.makeText(context, "登录失败，请使用其他方式或联系开发人员",
                                    Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "登录失败，请使用其他方式或联系开发人员",
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(context, "登录失败，请使用其他方式", Toast.LENGTH_SHORT)
                            .show();
                    Log.v("thirdLogin", "StaticInApp.GET_THIRD_REG_INFO/" + e.getMessage());
                }

                handlerUI.sendMessage(regMessage);
            }

        }
    }

    HttpResponseListener userListener = new HttpResponseListener() {

        @Override
        public void onSuccess(Object result) {
            ListData<SociaxItem> list = (ListData<SociaxItem>) result;
            if (list != null && list.size() == 1) {
                PrefUtils.setPrefIsLocalRegister(Thinksns.getContext(), true); // 显示推荐向导
                ModelUser loginedUser = (ModelUser) list.get(0);
                Thinksns.setMy(loginedUser);
                UserSqlHelper db = UserSqlHelper.getInstance(context);
                db.addUser(loginedUser, true);
                Message regMessage = new Message();
                regMessage.what = StaticInApp.DO_THIRD_LOGIN;
                regMessage.arg1 = MSG_SUCCESS;
                handlerUI.sendMessage(regMessage);
            }
        }

        @Override
        public void onError(Object result) {
            Message regMessage = new Message();
            regMessage.what = StaticInApp.DO_THIRD_LOGIN;
            regMessage.arg1 = MSG_SUCCESS;
            handlerUI.sendMessage(regMessage);
        }
    };

    /**
     * 执行分享微博
     *
     * @param weibo 被分享的微博
     */
    public void doShareWeibo(ModelWeibo weibo) {
        // 分享内容 URL
        String str_shareUrl = ApiHttpClient.PROTOCOL_FORMAT + mHost + context.getResources().getString(R.string.w3g_address_of_shareSDK_weibo) + weibo.getWeiboId();
        String from_who  = Thinksns.getMy() == null ? weibo.getUsername() : Thinksns.getMy().getUserName();
        // 标题
        String str_shareTitle = "来自" + from_who + "的分享";
        // 内容
        String str_shareContent = weibo.getContent() == null ? "来自" + context.getResources().getString(R.string.app_name) + "的分享" : weibo.getContent();
        String str_shareVedioUrl = null;// 分享的视频地址
        String img_shareImgUrl = null;// 分享内容的图片地址
        // 展示的图片，如果有视频的话获取视频地址,以及预览的图片地址
        if (weibo.hasVideo()) {
            ModelVideo myVideo = weibo.getAttachVideo();
            if (myVideo.getVideoDetail() != null) {
                str_shareVedioUrl = myVideo.getVideoDetail();
            }
            img_shareImgUrl = myVideo.getVideoImgUrl(); // 预览图片
        } else if (weibo.hasImage()) {
            // 没有视频如果有图片的话使用图片地址
            img_shareImgUrl = ((ModelImageAttach) weibo.getAttachImage().get(0)).getMiddle();
        } else {
            // 既没有视频也没有图片的话使用ts的图标，可能需要进一步细化，考虑使用微博的转发微博的图片
            img_shareImgUrl = weibo.getUserface();
        }
        doShare(pf, str_shareUrl, str_shareTitle, str_shareContent
                , img_shareImgUrl, str_shareVedioUrl);


    }

    /**
     * 执行分享链接
     */
    public void doShareStr() {

        ModelUser user = Thinksns.getMy();
        if (user == null) {
            return;
        }
        String str_shareTitle = null;// 分享的标题，一般 “来自uname的分享”
        String str_shareContent = null;// 分享的内容
        String str_shareUrl = ApiHttpClient.PROTOCOL_FORMAT + mHost + context.getResources().getString(R.string.w3g_address_of_share_erweima) + user.getUid();
        String img_shareImgUrl = user.getUserface();// 分享内容的图片地址
        // 标题
        str_shareTitle = user.getUserName();
        // 内容
        str_shareContent = "点击查看个人主页";
        doShare(pf, str_shareUrl, str_shareTitle, str_shareContent
                , img_shareImgUrl, null);

    }

    /**
     * 执行分享活动
     *
     * @param detail 被分享的活动
     */
    public void doShareEvent(ModelEventDetail detail) {
        String str_shareVedioUrl = null;// 分享的视频地址
        String img_shareImgUrl = detail.getImage();// 分享内容的图片地址
        String str_shareTitle = detail.getName();// 分享的标题，一般 “来自uname的分享”
        String str_shareContent = Html.fromHtml(detail.getContent()).toString();// 分享的内容
        String str_shareUrl = ApiHttpClient.PROTOCOL_FORMAT + mHost + mContext.getString(R.string.w3g_address_of_share_event);//测试链接

        doShare(pf, str_shareUrl, str_shareTitle, str_shareContent
                , img_shareImgUrl, str_shareVedioUrl);
    }

    /**
     * 执行分享帖子
     *
     * @param post 被分享的帖子
     */
    public void doSharePost(ModelPost post) {
        String str_shareVedioUrl = null;// 分享的视频地址
        String img_shareImgUrl = null;// 分享内容的图片地址
        String str_shareTitle = post.getTitle();// 分享的标题，一般 “来自uname的分享”
        String str_shareContent = UnitSociax.filterHtml(post.getContent());// 分享的内容
        String str_shareUrl = ApiHttpClient.PROTOCOL_FORMAT + mHost + context.getResources().getString(R.string.w3g_address_of_shareSDK_post) + post.getPost_id();

        if (!TextUtils.isEmpty(post.getAttach()) && !"null".equals(post.getAttach().toLowerCase())) {
            img_shareImgUrl = post.getAttach();
        } else {
            img_shareImgUrl = post.getUface();
        }
        doShare(pf, str_shareUrl, str_shareTitle, str_shareContent
                , img_shareImgUrl, str_shareVedioUrl);
    }

    public void doShareUrl(ModelInformationCateList cateList) {
        ModelUser user = Thinksns.getMy();
        if (user == null) {
            return;
        }
        String str_shareTitle = cateList.getSubject();// 分享的标题，一般 “来自uname的分享”
        String str_shareContent = null;// 分享的内容
        String img_shareImgUrl = user.getUserface();// 分享内容的图片地址
        // 内容
        str_shareContent = UnitSociax.filterHtml(cateList.getAbstracts());

        doShare(pf, cateList.getUrl(), str_shareTitle, str_shareContent
                , img_shareImgUrl, null);
    }

    /**
     * 真正执行分享 8.3.2017
     * @param platform  分享平台
     * @param str_shareUrl 分享地址
     * @param str_shareTitle  显示 title
     * @param str_shareContent 内容
     * @param img_shareImgUrl 图片地址
     * @param str_shareVedioUrl 视屏地址
     */
    private void doShare(Platform platform, String str_shareUrl, String str_shareTitle, String str_shareContent
            , String img_shareImgUrl, String str_shareVedioUrl) {

        mOnekeyShare.setTitle(str_shareTitle);
        mOnekeyShare.setText(str_shareContent); // 所有平台都需要，微博需要被覆盖，加上url
        if (!TextUtils.isEmpty(img_shareImgUrl)) {
            mOnekeyShare.setImageUrl(img_shareImgUrl);
        }
        if (QQ.NAME.equals(platform.getName()) || QZone.NAME.equals(platform.getName())) {
            // QQ分享
            mOnekeyShare.setTitleUrl(str_shareUrl);// 标题的超链接,点击之后直接跳转到指定的页面
            mOnekeyShare.setSite(context.getResources().getString(R.string.app_name));
            mOnekeyShare.setSiteUrl(mHost);
            if (!TextUtils.isEmpty(str_shareVedioUrl)){
                mOnekeyShare.setVideoUrl(str_shareVedioUrl);
            }
        } else if (Wechat.NAME.equals(platform.getName()) || WechatMoments.NAME.equals(platform.getName())) {
            // 分享到微信
            mOnekeyShare.setUrl(str_shareUrl);// 超链接，点击之后进入3G网站
            if (!TextUtils.isEmpty(str_shareVedioUrl)) {// 分享视频
                mOnekeyShare.setVideoUrl(str_shareVedioUrl); // 分享网络视频，分享本地视频不是这个
            }
        } else if (SinaWeibo.NAME.equals(platform.getName())) {
            /*微博要是设置了视屏地址，图片和视屏都不会显示，而是多了一个链接，所以这里不设置视屏地址*/
            if (!TextUtils.isEmpty(str_shareContent)) {
                if (str_shareContent.length() > 140) {
                    int urlLength = "...  ".length();
                    int contentSize = 140 - urlLength;
                    str_shareContent = str_shareContent.substring(0, contentSize) + "...  " + str_shareUrl;
                } else {
                    str_shareContent += " " + str_shareUrl;
                }
                mOnekeyShare.setText(str_shareContent);
                /*必须设置，不知什么原因，微博不设置此的话，在nexus手机上分享提示 content can not be null*/
                mOnekeyShare.setUrl(" ");
            }
        }
        // 设置分享事件回调
        mOnekeyShare.setCallback(mPlatformActionListener);
        mOnekeyShare.show(mContext);
    }

    private PlatformActionListener mPlatformActionListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            Message msg = new Message();
            msg.what = StaticInApp.DO_THIRD_SHARE;
            msg.arg1 = MSG_SUCCESS;
            handlerUI.sendMessage(msg);

            String[] platInfo = new String[3];
            platInfo[0] = getTypeName(platform);
            if (platInfo[0] == null)
                return;
            platInfo[1] = platform.getDb().getUserId();
            platInfo[2] = platform.getDb().getToken();
            Log.v("share", "doSharePost/onComplete/plat info[0]/" + platInfo[0] + "/info[1]/" + platInfo[1] + "/info[2]/" + platInfo[2]
                    + "/i/" + i + "/hashMap/" + hashMap.toString());
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            Log.v("share","onError" + throwable.getMessage());
        }

        @Override
        public void onCancel(Platform platform, int i) {
            Message msg = new Message();
            msg.what = StaticInApp.DO_THIRD_SHARE;
            msg.arg1 = MSG_CANCEL;
            handlerUI.sendMessage(msg);
        }
    };
}
